package com.example.bandlock;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class ChatListAdapter extends ArrayAdapter<ChatListItem> {

	public ChatListAdapter(Context context, List<ChatListItem> items) {
		super(context, R.layout.chat_list_item, items);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;

		if (convertView == null) {
			// inflate the GridView item layout
			LayoutInflater inflater = LayoutInflater.from(getContext());
			convertView = inflater.inflate(R.layout.chat_list_item, parent,
					false);

			// initialize the view holder
			viewHolder = new ViewHolder();
			viewHolder.bandnum = (TextView) convertView
					.findViewById(R.id.bandnum);
			viewHolder.txt = (TextView) convertView.findViewById(R.id.chat_txt);
			viewHolder.cBox = (CheckBox) convertView
					.findViewById(R.id.checkBox);
			convertView.setTag(viewHolder);
		} else {
			// recycle the already inflated view
			viewHolder = (ViewHolder) convertView.getTag();
		}

		// update the item view
		ChatListItem item = getItem(position);
		viewHolder.bandnum.setText(item.bandnum);
		viewHolder.txt.setText(item.txt);
		viewHolder.cBox.setChecked(item.checked);

		return convertView;
	}

	/**
	 * The view holder design pattern prevents using findViewById() repeatedly
	 * in the getView() method of the adapter.
	 * 
	 * @see http 
	 *      ://developer.android.com/training/improving-layouts/smooth-scrolling
	 *      .html#ViewHolder
	 */
	private static class ViewHolder {
		TextView bandnum;
		TextView txt;
		CheckBox cBox;
	}
}